#include "main.h"
#include "RFID.h"
#include "usart.h"

uint8_t recvBuff[30];
uint8_t awakeSend[] = {0x55,0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x03,0xFD,0xD4,0x14,0x01,0x17,0x00};
uint8_t awakeRecv[] = {0x00,0x00,0xFF,0x02,0xFE,0xD5,0x15,0x16,0x00};
uint8_t searchSend[] = {0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD4, 0x4A, 0x02, 0x00, 0xE0, 0x00};
//uint8_t searchSend[] = {0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD4, 0x4A, 0x02, 0x00, 0xE0, 0x00};
uint8_t cardID[7];
_Bool rfidFlag = 0;
extern _Bool stopFlag;

uint8_t nfcInit(void)
{
    uint8_t i = 0;
    HAL_UART_Transmit(&huart2, awakeSend, sizeof(awakeSend)/sizeof(uint8_t), 0xFFFF);
    HAL_UART_Receive(&huart2, recvBuff, 15, 0xFFFF);
	
	for(i = 0; i < 30; i++)
    {
        recvBuff[i] = 0;
    }
	
	HAL_UART_Transmit(&huart2, searchSend, sizeof(searchSend)/sizeof(uint8_t), 0xFFFF);
	HAL_UART_Receive(&huart2, recvBuff, 6, 0xFFFF);
    HAL_UART_Receive_IT(&huart2, recvBuff, 22);
//    for(i = 0; i < 9; i++)
//    {
//        if(recvBuff[i] != awakeRecv[i])
//        {
//            return 0;
//        }
//    }
    
    return 1;
}

//uint8_t nfcSearch(void)
//{
//    uint8_t i = 0;
//    HAL_UART_Transmit(&huart2, searchSend, sizeof(searchSend)/sizeof(uint8_t), 0xFFFF);
////	HAL_Delay(1);
//    HAL_UART_Receive(&huart2, recvBuff, 6, 0xFFFF);
//	if(recvBuff[4] == 0xFF)
//	{
//		HAL_UART_Receive(&huart2, recvBuff, 22, 0xF);
//		if(recvBuff[
//	}
//    
//    if(recvBuff[4] == 0x00)
//    {
//        return 0;
//    }
//    else
//    {
//        HAL_UART_Receive(&huart2, recvBuff, 22, 0xF);
//        
//        for(i = 0; i < 7; i++)
//        {
//            cardID[i] = recvBuff[i + 13];
//        }
//		rfidFlag = 1;
//		
//		HAL_UART_Transmit(&huart1, cardID, 7, 0xFFFF);
//    }
//    
//    
//    
//    return 1;
//}
